﻿using System.Drawing;
using System.Windows.Forms;

namespace Systemathics.Applications
{
    public sealed partial class TraderApplication : Form
    {
        private const int UPDATE_MILLISECONDS = 333;

        public TraderApplication()
        {
            InitializeComponent();
            Dock = DockStyle.Fill; 
            DoubleBuffered = true;

            ShowIcon = true;
            SizeGripStyle = SizeGripStyle.Hide;
            MaximizeBox = true;
            MinimizeBox = true;
            StartPosition = FormStartPosition.CenterScreen;
            FormBorderStyle = FormBorderStyle.Sizable;
            ControlBox = true;
            Opacity = 0.9;

            RepaintTimer.Tick += RepaintTimerTick;
            RepaintTimer.Interval = UPDATE_MILLISECONDS;
        }

        private void RepaintTimerTick(object sender, System.EventArgs e)
        {
            if (traderControl.Strategy.isRunning)
                traderControl.UpdateTraderControl();

            strategyContainerControl.UpdateStrategyContainer();
        }

        private void TraderApplication_Load(object sender, System.EventArgs e)
        {
            strategyContainerControl.OnStrategySelected += StrategyContainerControlOnStrategySelected;
        }

        private void StrategyContainerControlOnStrategySelected(object sender, System.EventArgs e)
        {
            if (strategyContainerControl.Strategy != null)
            {
                if (RepaintTimer.Enabled)
                RepaintTimer.Stop();

                traderControl.Clear();
                traderControl.Strategy = strategyContainerControl.Strategy;

                RepaintTimer.Start();
            }
        }
        protected override void OnHandleDestroyed(System.EventArgs e)
        {
            RepaintTimer.Stop();
            RepaintTimer.Tick -= RepaintTimerTick;
            strategyContainerControl.OnStrategySelected -= StrategyContainerControlOnStrategySelected;
            traderControl.Dispose();
            strategyContainerControl.Dispose();
            base.OnHandleDestroyed(e);
        }
    }
}
